package com.pswv2.backstage.more.wallet.account.details.action;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pswv2.backstage.more.wallet.account.details.service.AccountDetailsService;
import com.pswv2.bub.bsc.dao.po.AccountDetails;
import com.pswv2.bub.bsc.dao.po.AnswerRequirements;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;

@Controller
@RequestMapping(value = "accountDetailsAction", produces = { "text/json;charset=UTF-8" })
public class AccountDetailsAction {
	private static Class<?> poc = AccountDetails.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private AccountDetailsService accountDetailsService;
	/**
	 * 通过用户Id查询账单
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByUserId")
	public String queryByURIdUId(int userId) {
		List<AccountDetails> lsar=extraSpringHibernateTemplate.findAllByPropEq(poc, "userId", userId);
		if(lsar.size()>0){
			return CommonResp.buildSuccessResp(lsar).toJsonString();
		}else{
			return CommonResp.buildSuccessResp().toJsonString();
		}
	}
	/**
	 * 通过用户Id查询积分账单
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByUserIdPoints")
	public String queryByUserIdPoints(int userId) {
		List<AccountDetails> lsar =(List<AccountDetails>) extraSpringHibernateTemplate.getHibernateTemplate()
				.find("From AccountDetails where userId=? and type=?",
						userId,AccountDetails.pionts);
		if(lsar.size()>0){
			return CommonResp.buildSuccessResp(lsar).toJsonString();
		}else{
			return CommonResp.buildSuccessResp().toJsonString();
		}
	}
	/**
	 * 通过用户Id查询现金账单
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByUserIdPrice")
	public String queryByUserIdPrice(int userId) {
		List<AccountDetails> lsar =(List<AccountDetails>) extraSpringHibernateTemplate.getHibernateTemplate()
				.find("From AccountDetails where userId=? and type=?",
						userId,AccountDetails.money);
		if(lsar.size()>0){
			return CommonResp.buildSuccessResp(lsar).toJsonString();
		}else{
			return CommonResp.buildSuccessResp().toJsonString();
		}
	}
	/**
	 * 添加账单账单
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public String add(int userId,double amount,int proceedsPaid,String matter) {
		AccountDetails ad=new AccountDetails();
		ad.setUserId(userId);
		ad.setAmount(amount);
		long time=new Date().getTime();
		ad.setTime(time);
		ad.setProceedsPaid(proceedsPaid);
		ad.setMatter(matter);
		extraSpringHibernateTemplate.getHibernateTemplate().save(ad);
		return CommonResp.buildSuccessResp().toJsonString();
	}

}
